package com.huawei.chengdu.meetingdemo.view.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.LogUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DialPadDialog extends Dialog {
    private ImageView dialogClose;
    private TextView inputText;
    

    public DialPadDialog(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dial_pad_dialog);
        initView();
        initListner();
    }

    @Override
    public void show() {
        setCanceledOnTouchOutside(false);
        getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        super.show();
    }

    private void initView() {
        dialogClose = findViewById(R.id.dial_pad_close);
        inputText = findViewById(R.id.input_text);
    }

    private void initListner() {
        dialogClose.setOnClickListener(v -> dismiss());
        findViewById(R.id.mid_input_num_1).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_2).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_3).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_4).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_5).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_6).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_7).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_8).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_9).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_0).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_sign).setOnClickListener(view -> keyboardOnClick(view));
        findViewById(R.id.mid_input_num_star).setOnClickListener(view -> keyboardOnClick(view));
        inputText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    return;
                }
                ConfSdkPresenter.secondCall(s.charAt(s.length() - 1));
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
        ignoreInputMethod();

    }

    private void keyboardOnClick(View view) {
        CharSequence clickText = ((TextView)view).getText();
        String newText = inputText.getText().toString() + clickText;
        inputText.setText(newText);
    }

    private void ignoreInputMethod() {
        // 设置不显示输入法
        try {
            Class<EditText> cls = EditText.class;
            Method setSoftInputShownOnFocus;
            // setShowSoftInputOnFocus为设置显示输入法的方法名，固定字段请勿修改
            setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
            setSoftInputShownOnFocus.setAccessible(true);
            setSoftInputShownOnFocus.invoke(inputText, false);
        } catch (IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | NoSuchMethodException e) {
            LogUtil.error("setShowSoftInputOnFocus error");
        }
    }
}